package com.dbpms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dbpms.mapper.MenuPowerMapper;
import com.dbpms.mapper.StudentManageMapper;
import com.dbpms.mapper.UserMapper;
import com.dbpms.pojo.StudentInfo;
import com.dbpms.pojo.User;
import com.dbpms.pojo.menuPower.menupowerinfo;
import com.dbpms.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private StudentManageMapper studentManageMapper;
    @Resource
    private MenuPowerMapper menuPowerMapper;

    /**
     * 登录判断
     * @param user_account
     * @param user_pwd
     * @return
     */
    @Override
    public User login(String user_account, String user_pwd) {
        return lambdaQuery()
                .eq(User::getUserAccount, user_account)
                .eq(User::getUserPwd, user_pwd)
                .one();
    }

    /**
     * 用户注册
     * @param stu_id  ： 学生id
     * @param stu_name  ：学生姓名
     * @param user_account  ：用户账户
     * @param user_pwd ： 用户密码
     * @return   1 :注册成功   -1：账号已存在    0：该学号已注册
     */
    @Override
    public int addRegisterUser(int stu_id,String stu_name, String user_account, String user_pwd) {
        int pd = 0;
        try {
            User user = lambdaQuery()
                    .eq(User::getUserStudentId, stu_id)
                    .one();
            // 说明用户表中不存在该学生账户，允许注册
            if (user == null){
                // 补充数据
                user = new User();
                user.setUserAccount(user_account);
                user.setUserPwd(user_pwd);
                user.setUserPower(0);
                user.setUserDetail(stu_name);
                user.setUserStudentId(stu_id);
                // 执行 - 保存
                save(user);
                // 执行 - 该学生信息表加上新账户的user_id
                int user_id = lambdaQuery()
                        .eq(User::getUserStudentId, stu_id)
                        .one().getUserId();
                UpdateWrapper<StudentInfo> wrapper = new UpdateWrapper<StudentInfo>()
                        .set("student_user_id",user_id)
                        .eq("student_id",stu_id);
                studentManageMapper.update(null,wrapper);
                pd = 1;
            }
        }catch (Exception e){
            e.printStackTrace();
            pd = -1;
        }
        return pd;
    }

    /**
     * 判定学号是否存在
     * @param number
     * @return
     */
    @Override
    public StudentInfo ifExitUser(String number) {
        QueryWrapper<StudentInfo> wrapper = new QueryWrapper<StudentInfo>()
                .eq("student_number",number);
        return studentManageMapper.selectOne(wrapper);
    }

    /**
     * 获得菜单权限列表
     * @param role
     * @return
     */
    @Override
    public List<menupowerinfo> getMenuPowerInfo(int role) {
        List<menupowerinfo> info;
        LambdaQueryWrapper<menupowerinfo> wrapper;
        // 管理员菜单列表
        if (role == 1){
            // SELECT * FROM `menupowerinfo2` where menu_power_user_role = 1
            wrapper = new LambdaQueryWrapper<menupowerinfo>()
                    .eq(menupowerinfo::getMenuPowerUserRole,1);

        }else{
            // SELECT * FROM `menupowerinfo2` where menu_power_stu_role = 1 ORDER BY menu_power_id DESC
            wrapper = new LambdaQueryWrapper<menupowerinfo>()
                    .eq(menupowerinfo::getMenuPowerStuRole,1)
                    .orderByAsc(menupowerinfo::getMenuPowerId);
        }
        info = menuPowerMapper.selectList(wrapper);

        return info;
    }


    /**
     * 修改密码
     * @param old_pwd  ：旧密码
     * @param new_pwd  ：新密码
     * @param user_id  ：用户id
     * @return  1:修改成功  -1：旧密码错误    -2：修改失败
     */
    @Override
    public int updatePwd(String old_pwd, String new_pwd, int user_id) {

        // 执行 - 判断旧密码是否正确  SELECT * FROM `userinfo` where user_pwd = ? and user_id = ?
        User user = lambdaQuery().eq(User::getUserPwd,old_pwd).eq(User::getUserId,user_id).one();

        if (user == null) // 旧密码错误
            return -1;

        // 执行 - 修改密码  update userinfo set user_pwd=? WHERE user_id = ?
        boolean pd = lambdaUpdate()
                .set(User::getUserPwd,new_pwd)
                .eq(User::getUserId,user_id)
                .update(); // 执行修改
        if (pd) // 修改成功
            return 1;
        // 修改失败
        return -2;
    }

    @Override
    public int updateUserInfo(StudentInfo info, String fileName) {
        return 0;
    }
}
